/**
 * 初始化预期结果输入框
 * @param
 * @return
 */
var testCase = {
    initExpectResult: function (expectResult) {
        if (typeof(expectResult) == "object") {
            expectResult = JSON.stringify(expectResult);
        }
        $("#expectResultValue").remove();
        $("#expectResultDiv").empty();
        /**如果是json格式,则初始化jsonEditor**/
        if (null != expectResult && (expectResult.endWith("}") || expectResult.endWith("]"))) {
            $("#expectResultDiv").append(" <div  id=\"expectResultValue\"  class=\"form-control\"\n" +
                "  style=\"min-height: 60%;max-width: 100%;min-width: 100%\"></div>");
            initJson("expectResultValue");
            var o = $.parseJSON(expectResult);
            jsonEditor.set(o);
        } else if (typeof (expectResult) == "undefined") {
            /**新增默认使用文本**/
            $("#expectResultDiv").append(" <textarea  id=\"expectResultValue\"  class=\"form-control\"\n" +
                "  style=\"min-height: 60%;max-width: 100%;min-width: 100%\"></textarea>");
        } else if (null != expectResult && (expectResult.trim().startsWith("<"))) {
            /**xml格式化**/
            var xml;
            try {
                xml = formatXml(expectResult);
            } catch (e) {
                xml = expectResult;
            }
            $("#expectResultDiv").append(" <textarea  id=\"expectResultValue\"  class=\"form-control\"\n" +
                "  style=\"min-height: 60%;max-width: 100%;min-width: 100%\">" + xml + "</textarea>");
        }
        else {
            /**新增默认使用文本**/
            $("#expectResultDiv").append(" <textarea  id=\"expectResultValue\"  class=\"form-control\"\n" +
                "  style=\"min-height: 60%;max-width: 100%;min-width: 100%\">" + expectResult + "</textarea>");
        }
    },

    /**
     * 获取预期结果数据类型，如果是文本域读取数据,
     * 如果不是说明使用到了jsonEditor插件获取预期结果的值,写入隐藏input标签expectResult里面
     * @param
     * @return
     */
    writeExpectResult: function () {
        var tagName = $("#expectResultValue")[0].tagName;
        if (tagName == "TEXTAREA") {
            $("#expectResult").val($("#expectResultValue").val());
        } else {
            /**因为jsonEditor不支持非json的格式所以get会出现异常
             如果出现异常直接读取div里面的内容**/
            var jsonStr;
            try {
                var json = jsonEditor.get();
                jsonStr = JSON.stringify(json, null, 2);
            } catch (e) {
                /**
                 * 不是正常的json会出现异常,通过其他方式取值
                 * **/
                jsonStr = $(".jsoneditor-text").val();
            }
            $("#expectResult").val(jsonStr);
        }
    },
    /**
     * 填写表达式到expressionValue里面
     */
    writeExpression: function () {
        var length = $("#expressionDiv").children('div').length;
        if (length == 0) {
            return;
        }
        var childrenDiv = $("#expressionDiv").children('div');
        var list = [];
        childrenDiv.each(function () {
            var i = $(this).attr("name");
            var value = $("#expressionValue" + i).val();
            list.push(value);
        })
        $("#matchExpression").val(JSON.stringify(list));
    },
};
/**
 * 增加一行表达式
 */
$("#addExpressionButton").click(function () {
    var keyIndexStr = $("#expressionIndex").val();
    var deleteId = "expression" + keyIndexStr;
    var keyIndex = parseInt(keyIndexStr);
    var div = "<div style='margin-bottom: 5px' name=" + keyIndex + " id=expression" + keyIndex + ">" +
        "<input type='text' id=expressionValue" + keyIndex + " style='margin-left: 5%;width: 80%;border:1px solid #378888;font-size:25px' placeholder='表达式'>" +
        "&nbsp;&nbsp;" +
        "<a type ='button'  onclick='deleteExpressionDiv(\"" + deleteId + "\")'><img src='/images/delete.png'></a></div>";
    $("#expressionDiv").append(div);
    keyIndex += 1;
    $("#expressionIndex").val(keyIndex);
})
/**
 * 删除一行表达式
 */
deleteExpressionDiv = function (id) {
    $("#" + id).remove();
}
/**
 * 绑定提交事件,主要是处理keyValue入参和预期结果表达式和预期结果
 */
$("#testCaseSubmit").submit(function (e) {
    oKeyValueUtil.writeParam();
    testCase.writeExpectResult();
    testCase.writeExpression();
});
/**
 * 绑定测试表达式事件
 */
$("#testExpressionButton").click(function () {
    testCase.writeExpectResult();
    testCase.writeExpression();
    var expectResult = $("#expectResult").val();
    var matchExpression = $("#matchExpression").val();
    var matchType = $("#matchType").val();
    if (null == expectResult || expectResult == "") {
        toastr.warn("预期结果不能为空!");
        return;
    }
    if (null == matchExpression || matchExpression == "") {
        toastr.warn("表达式不能为空!");
        return;
    }
    $.ajax({
        url: "/testCaseController/checkExpression",
        type: "post",
        dataType: "json",
        async: true,
        data: {
            expectResult: expectResult,
            matchExpression: matchExpression,
            matchType: matchType
        },
        beforeSend: function () {
            $("#wait").show();
            $("#testExpressionButton").attr("disabled", "disabled")
        },
        success: function (result) {
            $("#wait").hide();
            $("#testExpressionButton").removeAttr("disabled");
            $(".modal-body").empty();
            if (result.success) {
                var expectResult = result.obj;
                $(".modal-body").append("表达式正确:<span style='color: green'>" + expectResult + "</span>");
            } else {
                $(".modal-body").append("表达式异常:<span style='color: red'>" + result.msg + "</span>");
            }
            $("#myModal").modal();
        }, error: function (result) {
            $("#wait").hide();
            $("#testExpressionButton").removeAttr("disabled");
        }
    })

});
/**
 * 绑定回退事件
 */
$("#back").click(function () {
    window.location.href = "/testCaseController/testPage";
});
/**
 * 绑定预期结果表达式下拉框事件
 */
$("#matchType").change(function () {
    var value = $("#matchType").val();
    switch (value) {
        case "1":
        case "2":
        case "3":
            $("#match").hide();
            break;
        case "4":
        case "5":
        case "6":
            $("#match").show();
            break;
        default:
    }
})